import {ChangeDetectorRef, Component, Input, OnInit} from '@angular/core';
import {SFSchema} from "@delon/form";
import {ApiService} from "@core/services/api";
import {NzMessageService, NzModalRef} from "ng-zorro-antd";
import {HttpClient} from "@angular/common/http";
import {AreaService} from "../../area-manage/area.service";

@Component({
  selector: 'app-dept-edit',
  templateUrl: './dept-edit.component.html',
  styles: []
})
export class DeptEditComponent implements OnInit {
  @Input() type: 0 | 1;
  @Input()
  set _param(value) {
    if (!value) return;
    this.param = value;
    this.param.cityId = value.cities.map(item => item.cityId);
  };
  param: any;
  showForm = false;
  schema: SFSchema = {
    properties: {
      deptName: {
        type: 'string',
        title: '部门名称'
      },
      deptFullName: {
        type: 'string',
        title: '部门全称',
      },
      cityId: {
        type: 'string',
        title: '权限区域',
        enum: [],
        ui: {
          widget: 'tree-select',
          checkable: true,
          multiple: true,
          dropdownStyle: {
            'max-height': '300px'
          }
        }
      },
      status: {
        type: 'string',
        title: '状态',
        enum: [
          {label: '启用', value: true},
          {label: '停用', value: false},
        ]
      },
      deptRemark: {
        type: 'string',
        title: '备注'
      }
    },
    required: ['deptName', 'cityId']
  }
  submit(data): void {
    const body = JSON.parse(JSON.stringify(data))
    body.cities = data.cityId.map(item => {
      return {cityId: item};
    })
    delete body.cityId;
    body.deptType = '1';
    const url = this.type === 0 ? this.api.permision.add.url : this.api.permision.update.url;
    const method = this.type === 0 ? this.api.permision.add.method : this.api.permision.update.method
    this.http.request<any>(method, url,  { body}).subscribe(res => {
      console.log(res)
      if (res.status === '200' || res.status === '201') {
        this.msg.success('操作成功');
        this.modalRef.triggerOk();
      } else {
        this.msg.error(res.message)
      }
    })
  }
  constructor(
    private api: ApiService,
    private ref: ChangeDetectorRef,
    private msg: NzMessageService,
    private http: HttpClient,
    private modalRef: NzModalRef,
    private area: AreaService
  ) { }

  ngOnInit() {
    // this.area.getAreaTreeNode("").then(res => {
    //   console.log(res)
    //   this.schema.properties.cityId.enum = [res];
    //   this.ref.detectChanges();
    //   this.showForm = true;
    // })
    this.showForm = true;
  }
  searchData(e) {
    console.log(e);
    // 发送请求，将新增或修改后的数据提交上去
  }
}
